﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Web;
using System.Security.Principal;
using Bids.Core.DomainObjects;
using Bids.Data;
using Bids.Core.Common.Security;

namespace Bids.Service
{
    public interface IAuthenticationService
    {
        /// <summary>
        /// 写入表单登录
        /// </summary>
        /// <param name="user"></param>
        /// <param name="createPersistentCookie"></param>
        void SignIn(User user, bool createPersistentCookie);
        /// <summary>
        /// 注销
        /// </summary>
        void SignOut();
        /// <summary>
        /// 获取登录用户
        /// </summary>
        /// <returns></returns>
        User GetAuthenticatedUser();

        #region 管理员登录
        bool AdminLogin(string userName, string password);
        void AdminLogOut();
        Admin Admin { get; }
        bool AdminIsLogined { get; }
        #endregion
        #region 注册
        bool Register(User user);
        #endregion
    }
    public class FormAuthenticationService : IAuthenticationService
    {
        IRepository<Admin> _iRpoAdmin;
        IRepository<User> _iRpoUser;
        public FormAuthenticationService(IRepository<Admin> iRpoAdmin
            , IRepository<User> iRpoUser)
        {
            _iRpoAdmin = iRpoAdmin;
            _iRpoUser = iRpoUser;
        }
        #region 管理员登录
        public bool AdminLogin(string userName, string password)
        {
            password = password.ToMD5();
            var admin = _iRpoAdmin.Find(t => t.UserName == userName && t.Password == password);
            if (admin != null)
            {
                HttpContext.Current.Session["admin"] = admin;
                return true;
            }
            else
            {
                return false;
            }
        }
        public Admin Admin
        {
            get
            {
                if (AdminIsLogined)
                    return (Admin)HttpContext.Current.Session["admin"];
                return null;
            }
        }
        public bool AdminIsLogined { get { return HttpContext.Current.Session["admin"] != null; } }
        public void AdminLogOut()
        {
            HttpContext.Current.Session.Abandon();
        }
        #endregion
        #region 注册
        public bool Register(User user)
        {
            try
            {
                _iRpoUser.Add(user);
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
        #endregion
        /// <summary>
        ///写入表单登录
        /// </summary>
        /// <param name="user"></param>
        /// <param name="createPersistentCookie"></param>
        public void SignIn(User user, bool createPersistentCookie)
        {

            HttpContext.Current.Session["User"] = user;
            FormsAuthentication.SetAuthCookie(user.UserName, createPersistentCookie);
        }



        public void SignOut()
        {
            FormsAuthentication.SignOut();
            HttpContext.Current.Session.Remove("Member");
        }

        public User GetAuthenticatedUser()
        {
            if (((HttpContext.Current == null) || (HttpContext.Current.Request == null)) || (!HttpContext.Current.Request.IsAuthenticated || !(HttpContext.Current.User.Identity is FormsIdentity)))
            {
                return null;
            }
            User user = null;
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                user = (User)HttpContext.Current.Session["User"];
                if ((user != null) && user.UserName.ToLower().Equals(HttpContext.Current.User.Identity.Name.ToLower()))
                {
                    return user;
                }
                user = _iRpoUser.Find(t => t.UserName == HttpContext.Current.User.Identity.Name);

                HttpContext.Current.Session["User"] = user;
            }
            return user;
           
        }
    }
}
